package cn.wy.system.controller;

import cn.wy.basic.util.AjaxResult;
import cn.wy.basic.util.PageList;
import cn.wy.system.domain.Dictionarydetail;
import cn.wy.system.query.DictionarydetailQuery;
import cn.wy.system.service.DictionarydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping ("/dictionarydetail")
public class DictionarydetailController {
    @Autowired
    private DictionarydetailService dictionarydetailService;

    //查询全部
    @GetMapping
    public List<Dictionarydetail> findAll() {
        return dictionarydetailService.findAll();
    }
    //分页
    @PostMapping
    public PageList<Dictionarydetail> pageQuery(@RequestBody DictionarydetailQuery dictionarydetailQuery) {
        return dictionarydetailService.queryPage(dictionarydetailQuery);
    }
    //查询单个
    @GetMapping("/{id}")
    public Dictionarydetail findById(@PathVariable("id") Long id) {
        return dictionarydetailService.findOne(id);
    }
    //添加或者修改
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){
        try {
            if(dictionarydetail.getId() == null){
                dictionarydetailService.insert(dictionarydetail);
            }else {
                dictionarydetailService.update(dictionarydetail);
            }
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败！");
        }
    }
    //删除
    @DeleteMapping ("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionarydetailService.delete(id);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            dictionarydetailService.patchDel(ids);
            return new AjaxResult();
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
}
